package cn.dckeji.dcssm.controller;


import cn.dckeji.dcssm.common.model.ResultModel;
import cn.dckeji.dcssm.entity.Admin;
import cn.dckeji.dcssm.entity.Role;
import cn.dckeji.dcssm.service.AdminService;
import cn.dckeji.dcssm.service.RoleService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Kenny
 * @since 2019-02-26
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminService adminService;

    /**
     * 分页查询角色
     * @param pageNumber
     * @param pageSize
     * @param roleName
     * @return
     */
    @GetMapping("/page")
    public JSONObject page(Integer pageNumber, Integer pageSize, @RequestParam(value = "roleName", required = false) String roleName) {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.orderByDesc("gmt_create");
        if (StringUtils.isNotBlank(roleName)) {
            roleQueryWrapper.like("role_name", roleName);
        }
        IPage<Role> page = roleService.page(new Page<>(pageNumber, pageSize), roleQueryWrapper);
        List<Role> records = page.getRecords();

        List<Map> roleMapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(records)) {
            for (Role r:records) {
                Map<String, Object> stringObjectMap = BeanUtil.beanToMap(r);
                String roleId = r.getId();
                int count = adminService.count(new QueryWrapper<Admin>().eq("role_id", roleId));
                stringObjectMap.put("countAdmin", count);
                roleMapList.add(stringObjectMap);
            }
        }
        return ResultModel.page(page.getTotal(), roleMapList);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @PostMapping("/deleteById")
    public ResultModel<Object> deleteById(String roleId) {
        roleService.deleteById(roleId);
        return ResultModel.success();
    }

    /**
     * 查询角色对应的菜单
     * @param roleId
     * @return
     */
    @GetMapping("/getRoleMenu")
    public JSONObject getRoleMenu(@RequestParam(value = "roleId", required = false) String roleId) {
        return roleService.getRoleMenu(roleId);
    }

    /**
     * 保存
     * @param roleId
     * @param menuIds
     * @param roleName
     * @return
     */
    @PostMapping("/save")
    public ResultModel<Object> save(@RequestParam(required = false) String roleId,@RequestParam(required = false) String menuIds, String roleName) {
        roleService.save(roleId, menuIds, roleName);
        return ResultModel.success();
    }
}

